//
//  NewfeatureViewController.swift
//  CXSwiftWB
//
//  Created by meidaeriOS on 2017/6/1.
//  Copyright © 2017年 meidaer. All rights reserved.
//

import UIKit

class NewfeatureViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view.
    }
}

extension NewfeatureViewController: UICollectionViewDataSource, UICollectionViewDelegate {
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return 4
    }
    
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "CXNewfutrueCell", for: indexPath) as? CXCollectionViewCell
        // 字符串可以这样拼接
        cell?.img.image = UIImage(named: "new_feature_\(indexPath.row + 1)")
        cell?.btn.isHidden = true
        return cell!
    }
    
    func collectionView(_ collectionView: UICollectionView, didEndDisplaying cell: UICollectionViewCell, forItemAt indexPath: IndexPath) {
        
        // 获取当前展现在眼前cell对应的索引
        let path = collectionView.indexPathsForVisibleItems.last!
        
        let cell = collectionView.cellForItem(at: path) as! CXCollectionViewCell
        
        // 判断是否是最后一页
        if path.item == 3 {
            cell.btn.isHidden = false
            cell.btn.isUserInteractionEnabled = false
            cell.btn.transform = CGAffineTransform.init(scaleX: 0.0, y: 0.0)
            
            UIView.animate(withDuration: 2.0, delay: 0, usingSpringWithDamping: 0.7, initialSpringVelocity: 5.0, options: .allowAnimatedContent, animations: {
                cell.btn.transform = CGAffineTransform.identity
            }, completion: { (_) in
                cell.btn.isUserInteractionEnabled = true
            })
        }
        
    }
    
}

class CXCollectionViewCell: UICollectionViewCell {
    
    @IBOutlet weak var btn: UIButton!
    @IBOutlet weak var img: UIImageView!
    
    @IBAction func startBtnClick(_ sender: Any) {
        UIApplication.shared.delegate?.window!?.rootViewController = UIStoryboard(name: "Welcome", bundle: nil).instantiateInitialViewController()
    }
    
}

class CXCollectionFlowout: UICollectionViewFlowLayout {
    override func prepare() {
        super.prepare()
        itemSize = collectionView!.bounds.size
        print(itemSize)
        minimumLineSpacing = 0
        minimumInteritemSpacing = 0
        scrollDirection = .horizontal
        
        collectionView?.bounces = false
        collectionView?.showsHorizontalScrollIndicator = false
        collectionView?.showsVerticalScrollIndicator = false
        collectionView?.isPagingEnabled = true        
    }
}
